//
//  BKUserCustomBtn.m
//  bestkeep
//
//  Created by windCould on 16/12/12.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKUserCustomBtn.h"
#import "BKMyCommon.h"

@interface BKUserCustomBtn ()

@property (nonatomic,weak)NSTimer * countDownTimer;
@property (nonatomic,assign)NSInteger  countDownSeconds;


@end


@implementation BKUserCustomBtn


-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setBtnCSS];
    }
    return  self;
}

-(void)setBtnCSS
{
    self.backgroundColor = [UIColor whiteColor];
    self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [self setTitle:@"发送验证码" forState:UIControlStateNormal];
    self.layer.cornerRadius = 5;
    self.layer.borderWidth = 1.0;
    self.layer.borderColor = RGBCOLOR(27, 188, 156, 1).CGColor;
    [self setTitleColor:BK_MY_COLOR_01 forState:UIControlStateNormal];
    [self.titleLabel setFont:[UIFont systemFontOfSize:13]];
}


-(void)setBtnSelect:(BOOL)btnSelect
{
    if (btnSelect) {
        
        self.countDownSeconds = 60;//60秒倒计时
        [self setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        self.layer.borderColor = [UIColor grayColor].CGColor;
        self.userInteractionEnabled = NO;
        self.titleLabel.adjustsFontSizeToFitWidth = YES;
        [self setTitle:[NSString stringWithFormat:@"%ld秒",(long)self.countDownSeconds] forState:UIControlStateNormal];
        if (_countDownTimer == nil) {
            
            _countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES];
        }
        
    
    } else {
    
       [self cancelTimeCount];
        
    }
}

-(void)timeFireMethod{
   
    _countDownSeconds--;
    
    [self setTitle:[NSString stringWithFormat:@"%ld秒",(long)self.countDownSeconds] forState:UIControlStateNormal];
    if( self.countDownSeconds == 0){
        
        [self stopTime];
    }
    
}

-(void)cancelTimeCount
{
    if (self.countDownTimer.isValid) {
        
        
        [self.countDownTimer invalidate];
        self.countDownTimer = nil;
        self.userInteractionEnabled = YES;
        [self setTitle:@"发送验证码" forState:UIControlStateNormal];
        self.layer.borderColor = RGBCOLOR(27, 188, 156, 1).CGColor;
        [self setTitleColor:BK_MY_COLOR_01 forState:UIControlStateNormal];
        
    }
    
}

-(void)stopTime
{
    if (self.countDownTimer.isValid) {
        
        [self.countDownTimer setFireDate:[NSDate distantFuture]];
        [self.countDownTimer invalidate];
        self.countDownTimer = nil;
    }
    
        self.userInteractionEnabled = YES;
        [self setTitle:@"重新发送" forState:UIControlStateNormal];
        self.layer.borderColor = RGBCOLOR(27, 188, 156, 1).CGColor;
        [self setTitleColor:BK_MY_COLOR_01 forState:UIControlStateNormal];
}


-(void)dealloc
{
    [self.countDownTimer invalidate];
    self.countDownTimer = nil;
}
@end
